
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.IO;
using System.Text;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.common;
using SMAS;
using System.Resources;

namespace smasb2b
{
    public class VNDW0060 : BasePage
    {

        #region  Web Form 設計工具產生的程式碼

        //此為 Web Form 設計工具所需的呼叫。
        [System.Diagnostics.DebuggerStepThrough()]
        private void InitializeComponent()
        {

        }
        protected System.Web.UI.WebControls.Label lblINVOICES_DATE;
        protected ddscControlLibrary.DateCtrl txtINVOICES_DATE1;
        protected ddscControlLibrary.DateCtrl txtINVOICES_DATE2;
        protected System.Web.UI.WebControls.Label lblINVOICES_NO;
        protected System.Web.UI.WebControls.Label lblPAGEINDEX;
        protected ddscControlLibrary.InputBoxCtrl PageIndex;
        protected System.Web.UI.WebControls.Label lblPAGE1;
        protected System.Web.UI.WebControls.Label lblTOTAL;
        protected ddscControlLibrary.InputBoxCtrl PageCount;
        protected System.Web.UI.WebControls.Label lblPAGE;
        protected System.Web.UI.WebControls.ImageButton btnPREV;
        protected ddscControlLibrary.InputBoxCtrl GotoPage;
        protected System.Web.UI.WebControls.ImageButton BtnGo;
        protected System.Web.UI.WebControls.ImageButton BtnNext;
        protected System.Web.UI.HtmlControls.HtmlImage IMG1;
        protected System.Web.UI.HtmlControls.HtmlTable DataGrid;
        protected System.Web.UI.HtmlControls.HtmlTableRow DataGrid_foot;
        protected System.Web.UI.WebControls.Label lblVNDW0060;
        protected ddscControlLibrary.SubmitButtonCtrl btnSearch;
        protected System.Web.UI.WebControls.Label Label2;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.Label lblINVOICES_AMT;
        protected ddscControlLibrary.SubmitButtonCtrl btnInvoice;
        protected ddscControlLibrary.InputBoxCtrl txtINVOICES_NO;
        protected System.Web.UI.WebControls.Label lblPAY_DATE;
        protected System.Web.UI.WebControls.Label lblINVOICES_DATE1;
        protected System.Web.UI.WebControls.Label lblINVOICES_NO1;

        //注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
        //請勿刪除或移動它。
        private System.Object designerPlaceholderDeclaration;

        private void Page_Init(System.Object sender, System.EventArgs e)
        {
            //CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
            //請勿使用程式碼編輯器進行修改。
            InitializeComponent();
        }

        #endregion

        VENDOR LoginUser;
        private void Page_Load(System.Object sender, System.EventArgs e)
        {
            //在這裡放置使用者程式碼以初始化網頁
            try
            {

                if (!Page.IsPostBack)
                {
                    if (Session["langauage"] == "zh-tw")
                    {

                        this.btnInvoice.Style.Add("width", "85PX");
                    }
                    else
                    {
                        this.btnInvoice.Style.Add("width", "135PX");
                    }

                }
                else
                {
                    if (IsReSubmit)
                    {
                        DataGrid_DataBind();
                    }
                }
            }
            catch (Exception ex)
            {
                showException(ex.Message);
            }
            this.BtnNext.Click += BtnNext_Click;
            this.BtnGo.Click += BtnGo_Click;
            this.btnPREV.Click += btnPREV_Click;
            this.btnSearch.Click += btnSearch_Click;
        }

        private DataTable createErrorTable(string mError)
        {
            dbReturnInfo dbInfo = new dbReturnInfo("info");
            dbInfo.AddNewRow();
            dbInfo.setReturnDataValue("F", System.Convert.ToString(999), 0, mError);
            return dbInfo.getReturnDataTable();
        }

        public void DataGrid_DataBind()
        {
            //變數宣告
            LoginUser = GetLoginUser();
            DataSet ds = default(DataSet);
            dbAccess dba = new dbAccess();
            ArrayList m_arrParam = new ArrayList();
            string strCondition = "";
            int i = 0;
            int count = 2;
            string[] str_SQL_a = new string[count + 1];
            string[] str_SQL_b = new string[count + 1];

            try
            {
                //目前頁次指定Goto
                GotoPage.Text = (dynamic)PageIndex.Text;

                //預設查詢語法
                StringBuilder SqlCommandText = new StringBuilder();

                //Adams:2014/8/19
                //SqlCommandText.Append(" SELECT A.*, B.PAY_DATE ");
                SqlCommandText.Append(" SELECT A.INVOICES_NO,A.VENDOR_NO,A.INVOICES_DATE,A.INVOICES_AMT,A.REQ_DATE,A.REQ_DEPT,A.REQ_SEQ, ");
                SqlCommandText.Append(" CASE WHEN A.STATUS = '9' THEN B.PAY_DATE ELSE '' END PAY_DATE ");
                SqlCommandText.Append("   FROM (SELECT A.INVOICES_NO, ");
                SqlCommandText.Append("                A.VENDOR_NO, ");
                SqlCommandText.Append("                A.INVOICES_DATE, ");
                SqlCommandText.Append("                A.INVOICES_AMT, ");
                SqlCommandText.Append("                B.REQ_DATE, ");
                SqlCommandText.Append("                B.REQ_DEPT, ");
                SqlCommandText.Append("                B.REQ_SEQ,B.STATUS ");
                SqlCommandText.Append("           FROM B2B_INVOICE A, PUR_FIN_AP_DETAILS_INVOICE B ");
                SqlCommandText.Append("          WHERE A.INVOICES_NO = B.CERT_NO(+)) A, ");
                SqlCommandText.Append("        PUR_FIN_AP_MASTER B ");
                SqlCommandText.Append("  WHERE     A.REQ_DATE = B.REQ_DATE(+) ");
                SqlCommandText.Append("        AND A.VENDOR_NO = :LoginUser ");
                SqlCommandText.Append("        AND A.REQ_DEPT = B.REQ_DEPT(+) ");
                SqlCommandText.Append("        AND A.REQ_SEQ = B.REQ_SEQ(+) ");
                //SqlCommandText.Append("        AND A.REQ_SEQ = B.REQ_SEQ(+) AND B.STATUS='9' ");
                SqlCommandText.Append("  ");
                SqlCommandText.Append(" UNION  ");
                SqlCommandText.Append("  ");
                SqlCommandText.Append(" SELECT A.*, B.PAY_DATE ");
                SqlCommandText.Append("   FROM (SELECT A.INVOICES_NO, ");
                SqlCommandText.Append("                A.VENDOR_NO, ");
                SqlCommandText.Append("                A.INVOICES_DATE, ");
                SqlCommandText.Append("                A.INVOICES_AMT, ");
                SqlCommandText.Append("                B.REQ_DATE, ");
                SqlCommandText.Append("                B.REQ_DEPT, ");
                SqlCommandText.Append("                B.REQ_SEQ ");
                SqlCommandText.Append("           FROM B2B_INVOICE A, PUR_FIN_AP_DETAILS_INVOICE B ");
                SqlCommandText.Append("          WHERE A.INVOICES_NO = B.CERT_NO(+)) A, ");
                SqlCommandText.Append("        PUR_FIN_AP_MASTER B ");
                SqlCommandText.Append("  WHERE     A.REQ_DATE = B.REQ_DATE(+) ");
                SqlCommandText.Append("        AND A.VENDOR_NO = :LoginUser ");
                SqlCommandText.Append("        AND A.REQ_DEPT = B.REQ_DEPT(+) ");
                SqlCommandText.Append("        AND A.REQ_SEQ = B.REQ_SEQ(+) AND (B.PAY_DATE IS NULL OR B.PAY_DATE='') ");



                //Tony : 2_1.設定動態參數物件
                m_arrParam.Add(dba.GetOracleParam(":LoginUser", LoginUser.VENDOR_NO));

                StringBuilder SqlCommandCount = new StringBuilder();
                SqlCommandCount.Append("Select Count(INVOICES_NO) As row_total_count FROM(");
                SqlCommandCount.Append(" " + SqlCommandText.ToString() + " ");
                //SqlCommandCount.Append(" ) "); //Adams: 20140723 這個應該是要放到最後一個字元才對

                //Tony:我參考VNDW0050改

                //組合條件字串
                str_SQL_a[0] = txtINVOICES_NO.Text.Replace(",", "\'\'");

                //Tony:1_2修改SQL語法使用參數，不組字串
                //str_SQL_b[0] = string.Format("INVOICES_NO = \'{0}\' and ", str_SQL_a[0]);
                str_SQL_b[0] = "INVOICES_NO = :SQLA0 and ";

                //Tony : 2_2.設定動態參數物件
                if (str_SQL_a[0].Trim() != "")
                    m_arrParam.Add(dba.GetOracleParam(":SQLA0", str_SQL_a[0]));

                str_SQL_a[1] = txtINVOICES_DATE1.Text;
                string INVOICES_DATE2 = txtINVOICES_DATE2.Text;
                if (str_SQL_a[1] == INVOICES_DATE2)
                {
                    str_SQL_b[1] = "INVOICES_DATE = \'" + str_SQL_a[1] + "\' AND ";
                }
                else
                {
                    if (str_SQL_a[1] == "")
                    {
                        str_SQL_a[1] = INVOICES_DATE2;
                        str_SQL_b[1] = "INVOICES_DATE BETWEEN \'" + str_SQL_a[1] + "\' AND \'" + INVOICES_DATE2 + "\' AND ";
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(INVOICES_DATE2))
                        {
                            INVOICES_DATE2 = str_SQL_a[1];
                        }
                        str_SQL_b[1] = "INVOICES_DATE BETWEEN \'" + str_SQL_a[1] + "\' AND \'" + INVOICES_DATE2 + "\' AND ";
                    }
                }



                //查詢條件組合
                for (i = 0; i <= count - 1; i++)
                {
                    if (str_SQL_a[i].Trim() != "")
                    {
                        strCondition = strCondition + str_SQL_b[i];
                    }
                }

                if (!String.IsNullOrEmpty(strCondition))
                {
                    strCondition = " AND " + strCondition.Substring(0, strCondition.Length - 5);
                }

                int intCount = 0;
                int size = PageSize; // Update by titan 由BasePage設定

                // 計算資料總筆數
                //Tony : 3_1.修改執行方法Overload
                //DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount.ToString() + strCondition);
                //DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount.ToString() + strCondition, m_arrParam);
                //Adams:Modify FIX SQL  
                DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount.ToString() + strCondition + " )", m_arrParam);

                if (dsCount.Tables[0].Rows[0]["ReturnCode"] == "000")
                {
                    intCount = int.Parse(System.Convert.ToString(dsCount.Tables["Vendor_Count"].Rows[0]["row_total_count"]));
                    PageCount.Text = System.Convert.ToString(Math.Ceiling(System.Convert.ToDecimal(double.Parse((intCount).ToString()) / size)));
                }

                //Update By Titan 處理當總頁次小於目前頁次
                if ((int.Parse(PageCount.Text) < int.Parse(PageIndex.Text)) && int.Parse(PageCount.Text) > 0)
                {
                    // 總筆數(intCount)小於一頁，從第0筆開始顯示
                    PageIndex.Text = (dynamic)PageCount.Text;
                    GotoPage.Text = (dynamic)PageCount.Text;
                }
                // Update by titan 由BasePage設定
                // absolute position
                int position = ((int.Parse(PageIndex.Text)) - 1) * size;
                int maxrecord = position + size;


                //控制分頁按鈕
                ControlPageButton(DataGrid_foot);

                //執行資料庫語法
                //Tony : 3_2.修改執行方法Overload
                //ds = dba.runExecuteQuery("B2B_INVOICE", SqlCommandText.ToString() + strCondition + " Order By INVOICES_NO  ", maxrecord, position);
                ds = dba.runExecuteQuery("B2B_INVOICE", SqlCommandText.ToString() + strCondition, position, maxrecord, m_arrParam);

                if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
                {
                    HtmlTableRow tr = (HtmlTableRow)(DataGrid.FindControl("DataGrid_head"));
                    ArrayList ArrKey = new ArrayList();
                    ArrKey.Add("INVOICES_NO");

                    DataGrid_Bind_VNDW0060(DataGrid, ArrKey, ds.Tables["B2B_INVOICE"]);

                }
                else
                {
                    ResourceManager rm = GetResourceManager();
                    showException(rm.GetString("MSG069"));
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                ds.Dispose();
            }
        }

        private void btnSearch_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                PageIndex.Text = System.Convert.ToString(1);
                DataGrid_DataBind();
            }
            catch (Exception ex)
            {
                showException(ex.Message);
            }
        }

        private void BtnNext_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
        {
            try
            {
                PageIndex.Text = ((int.Parse(PageIndex.Text)) + 1).ToString();
                DataGrid_DataBind();
            }
            catch (Exception ex)
            {
                showException(ex.Message);
            }
        }

        private void BtnGo_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
        {
            ResourceManager rm = GetResourceManager();
            try
            {
                PageIndex.Text = (dynamic)GotoPage.Text;

                DataGrid_DataBind();
            }
            catch (Exception)
            {
                showException(rm.GetString("MSG057"));
            }
        }

        private void btnPREV_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
        {
            try
            {
                PageIndex.Text = ((int.Parse(PageIndex.Text)) - 1).ToString();
                DataGrid_DataBind();
            }
            catch (Exception ex)
            {
                showException(ex.Message);
            }
        }

        public void DataGrid_Bind_VNDW0060(HtmlTable DataGrid, ArrayList ArrKey, DataTable DataSource)
        {

            HtmlTableRow tr = (HtmlTableRow)(DataGrid.FindControl("DataGrid_head"));
            int i = 0;
            int j = 0;
            int k = 0;
            string Url = "";
            string className = "";
            ResourceManager rm = GetResourceManager();
            try
            {
                for (i = 0; i <= DataSource.Rows.Count - 1; i++)
                {
                    HtmlTableRow htr = new HtmlTableRow();
                    if (i % 2 == 0)
                    {
                        className = "evenRow";
                    }
                    else
                    {
                        className = "oddRow";
                    }
                    htr.Attributes.Add("class", className);
                    DataGrid.Rows.Add(htr);
                    for (j = 0; j <= tr.Cells.Count - 1; j++)
                    {
                        HtmlTableCell hd = new HtmlTableCell();
                        if (DataSource.Rows[i]["PAY_DATE"].ToString() != "")
                        {
                            tr.Cells[1].Attributes["class"] = "else";
                            tr.Cells[2].Attributes["class"] = "else";
                        }
                        else
                        {
                            tr.Cells[1].Attributes["class"] = "link";
                            tr.Cells[2].Attributes["class"] = "link";
                        }
                        switch (tr.Cells[j].Attributes["class"].ToLower())
                        {
                            case "link":
                                Url = string.Format("{0}?action_type={1}&GUID={2}", tr.Cells[j].Attributes["url"].ToString(), tr.Cells[j].ID, Guid.NewGuid());
                                for (k = 0; k <= ArrKey.Count - 1; k++)
                                {
                                    Url += "&" + ArrKey[k] + "=" + DataSource.Rows[i][ArrKey[k].ToString()].ToString();
                                }

                                hd.Width = tr.Cells[j].Width;
                                hd.InnerHtml = string.Format("<a href=\"javascript:window.showModalDialog(\'{0}\',this,\'{1}\', true)\">{2}</a>", Url, "dialogWidth=780px;dialogHeight=200px;scrollbars=yes;center=yes;border=thin;help=yes;status=yes;resizable=yes;", rm.GetString(tr.Cells[j].ID));
                                break;
                            case "tdtitle":
                                hd.Width = tr.Cells[j].Width;
                                hd.Align = "center";
                                hd.Attributes.Add("class", "detail");
                                hd.InnerText = System.Convert.ToString(DataSource.Rows[i][tr.Cells[j].Controls[1].ID.Replace("lbl", "")].ToString());
                                break;
                            case "tdtitle-right":
                                hd.Width = tr.Cells[j].Width;
                                hd.Align = "right";
                                hd.Attributes.Add("class", "detail");
                                hd.InnerText = System.Convert.ToString(DataSource.Rows[i][tr.Cells[j].Controls[1].ID.Replace("lbl", "")].ToString());
                                break;
                            default:
                                hd.Width = tr.Cells[j].Width;
                                hd.InnerText = rm.GetString(tr.Cells[j].ID);
                                break;
                        }
                        DataGrid.Rows[DataGrid.Rows.Count - 1].Cells.Add(hd);

                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }


        }


    }

}
